/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.service.impl;

import com.solid4j.auth.dao.BaseDao;
import com.solid4j.auth.model.User;
import com.solid4j.auth.page.Page;
import com.solid4j.auth.service.PageService;
import com.solid4j.auth.service.UserService;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

import javax.annotation.Resource;

/**
 * @author: solidwang
 * @date: 17/4/17
 */
@Component("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private BaseDao<User> baseDao;

    @Resource
    private PageService pageService;

    private Page<User> page;

    @Override
    public Integer save(User user) {
        return baseDao.save(user);
    }

    @Override
    public Page<User> getUserList(String condition, Integer currentPage, Integer pageSize, String orderBy) {
        if (page == null)
            page = new Page<User>();
        page.setObjName("User");
        page.setCondition(condition);
        page.setOrderBy("id desc");
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page = pageService.queryObjList(page);
        return page;
    }

    @Override
    public void delete(User user) {
        baseDao.delete(user);
    }

    @Override
    public User get(Integer id) {
        return baseDao.get(User.class, id);
    }

    @Override
    public User getByPassport(String passport) {
        List<User> userList = baseDao.getList("from User where passport = '" + passport + "'");
        if (userList != null) {
            return userList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void update(User user) {
        baseDao.update(user);
    }


}
